package com.example.demo.qy.controller;

import com.example.demo.common.utils.PageUtil;
import com.example.demo.common.utils.ResultUtil;
import com.example.demo.common.vo.PageVo;
import com.example.demo.common.vo.Result;
import com.example.demo.qy.model.Order;
import com.example.demo.qy.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "Order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/Order/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "通过id获取")
    public Result<Order> get(@PathVariable String id){
        Order entity = orderService.get(id);
        return new ResultUtil<Order>().setData(entity);
    }

    @RequestMapping(value = "/Order/getAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部数据")
    public Result<List<Order>> getAll(){
        List<Order> list = orderService.getAll();
        return new ResultUtil<List<Order>>().setData(list);
    }

    @RequestMapping(value = "/Order/getByPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分页获取")
    public Result<Page<Order>> getByPage(@ModelAttribute PageVo page){
        Page<Order> list = orderService.findAll(PageUtil.initPage(page));
        return new ResultUtil<Page<Order>>().setData(list);
    }

    @RequestMapping(value = "/Order/save",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<Order> save(@ModelAttribute Order entity){
        Order e = orderService.save(entity);
        return new ResultUtil<Order>().setData(e);
    }

    @RequestMapping(value = "/Order/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新数据")
    public Result<Order> update(@ModelAttribute Order entity){
        Order e = orderService.update(entity);
        return new ResultUtil<Order>().setData(e);
    }

    @RequestMapping(value = "/Order/del",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除数据")
    public Result<Object> delAll(@ModelAttribute Order entity){
        orderService.delete(entity);
        return new ResultUtil<Object>().setSuccessMsg("删除数据成功");
    }

    @RequestMapping(value = "/Order/delByIds",method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "批量通过ids删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids){
        for(String id:ids){
            orderService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量删除数据成功");
    }

}
